<template>
	<view class="content wrap">
		<u-form :model="form" ref="uForm"  :rules="rules">
			<u-form-item label="项目部" label-width="160" left-icon="home"><u-input v-model="form.operateName" type="select" @click="getO()"/></u-form-item>
			
			<u-form-item label="客户名称" label-width="170" left-icon="account"><u-input v-model="form.outCustomerName" type="select" @click="getC()"/></u-form-item>
			
			<u-form-item label="作业日期" label-width="170" left-icon="calendar"><u-input v-model="form.v1" type="select" @click="getT()"/></u-form-item>
			
			<u-form-item label="作业类型" label-width="150">
				<u-radio-group v-model="form.taskWork">
					<u-radio 
						@change="radioChange" 
						v-for="(item, index) in worklist" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="门店数" label-width="150" v-if="form.taskWork == '跟车'"><u-input v-model="form.v9" placeholder="请输入门店数"/></u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '卸车'">
				<u-radio-group v-model="form.type1">
					<u-radio 
						@change="radioChange4" 
						v-for="(item, index) in type1list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '装车'">
				<u-radio-group v-model="form.type2">
					<u-radio 
						@change="radioChange5" 
						v-for="(item, index) in type2list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '入库'">
				<u-radio-group v-model="form.type3">
					<u-radio 
						@change="radioChange6" 
						v-for="(item, index) in type3list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '移库'">
				<u-radio-group v-model="form.type4">
					<u-radio 
						@change="radioChange7" 
						v-for="(item, index) in type4list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '领料'">
				<u-radio-group v-model="form.type5">
					<u-radio 
						@change="radioChange8" 
						v-for="(item, index) in type5list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '废料'">
				<u-radio-group v-model="form.type6">
					<u-radio 
						@change="radioChange9" 
						v-for="(item, index) in type6list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '理库'">
				<u-radio-group v-model="form.type7">
					<u-radio 
						@change="radioChange10" 
						v-for="(item, index) in type7list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '批量作业'">
				<u-radio-group v-model="form.type8">
					<u-radio 
						@change="radioChange11" 
						v-for="(item, index) in type8list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '杂工'">
				<u-radio-group v-model="form.type9">
					<u-radio 
						@change="radioChange12" 
						v-for="(item, index) in type9list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '转仓装车'">
				<u-radio-group v-model="form.type1">
					<u-radio 
						@change="radioChange4" 
						v-for="(item, index) in type2list" :key="index" 
						
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="类别" label-width="150" v-if="form.taskWork == '转仓卸车'">
				<u-radio-group v-model="form.type1">
					<u-radio 
						@change="radioChange4" 
						v-for="(item, index) in type1list" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="车型" label-width="150" left-icon="car" v-if="form.taskWork == '卸车' || form.taskWork == '装车'">
				<u-radio-group v-model="form.carModel">
					<u-radio 
						@change="radioChange3" 
						v-for="(item, index) in carlist" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="车型" label-width="150" left-icon="car" v-if="form.taskWork == '转仓装车' || form.taskWork == '转仓卸车'">
				<u-radio-group v-model="form.carModel">
					<u-radio 
						@change="radioChange3" 
						v-for="(item, index) in carlist" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item label="核算方式" label-width="150">
				<u-radio-group v-model="form.settlementMode">
					<u-radio 
						@change="radioChange2" 
						v-for="(item, index) in modelist" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			
			<u-form-item v-if="form.settlementMode == '体积'" label="体积" label-width="150" prop="volume"><u-input v-model="form.volume" type="digit" placeholder="请输入体积"/></u-form-item>
			
			<u-form-item v-if="form.settlementMode == '数量'" label="数量" label-width="150" prop="nums"><u-input v-model="form.nums" type="digit" placeholder="请输入数量"/></u-form-item>
			
			<u-form-item v-if="form.settlementMode == '重量'" label="重量(吨)" label-width="150" prop="weights"><u-input v-model="form.weights" type="digit" placeholder="请输入重量"/></u-form-item>
			
			<u-form-item label="作业内容" label-width="150" prop="name"><u-input v-model="form.name" placeholder="请输入车牌号或工作内容等等"/></u-form-item>
			
			<u-form-item label="客户单号" label-width="150">
				<u-input v-model="form.customerNumber" type="textarea" placeholder="请输入客户单号，如多个逗号隔开"/>
				<u-icon @click="scan()" slot="right" name="scan" size="60"></u-icon>
			</u-form-item>
			
			<u-form-item label="备注" label-width="150"><u-input v-model="form.note" type="textarea" placeholder="请输入备注"/></u-form-item>
			
		</u-form>
		<u-button @click="submit">提交</u-button>
		<view>
			<u-select v-model="show" :list="clist" @confirm="confirm"></u-select>
		</view>
		<view>
			<u-select v-model="show2" :list="olist" @confirm="confirm2"></u-select>
		</view>
		<view>
			<u-calendar v-model="show3" mode="date" max-date="2070-01-01" @change="change"></u-calendar>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				form:{
				    name:'',
				    customerNumber:"",
				    outCustomerId:"",
					outCustomerName:"",
				    operateId:"",
				    operateName:"",
				    v1:"",
				    taskWork:"卸车",
				    type1:"成品",
				    type2:"成品",
				    type3:"成品",
				    type4:"成品",
				    type5:"人工",
				    type6:"打包",
				    type7:"盘点",
				    type8:"组套",
				    type9:"类别",
				    carModel:"海柜",
				    settlementMode:"体积",
				    volume:"",
				    nums:"",
				    weights:"",
				    hours:"",
				    note:"",
					v9:"",
				},
				type1list: [
					{
						name: '成品'
					},
					{
						name: '原材料手工'
					},
					{
						name: '原材料叉车'
					}
				],
				type2list: [
					{
						name: '成品'
					},
					{
						name: '原纸'
					},
					{
						name: '废料'
					}
				],
				type3list: [
					{
						name: '成品'
					},
					{
						name: '原纸'
					},
					{
						name: '废料'
					}
				],
				type4list: [
					{
						name: '成品'
					},
					{
						name: '原纸'
					},
				],
				type5list: [
					{
						name: '人工'
					},
					{
						name: '机械'
					}
				],
				type6list: [
					{
						name: '打包'
					},
					{
						name: '装袋'
					}
				],
				type7list: [
					{
						name: '盘点'
					},
					{
						name: '退货'
					},
					{
						name: '卫生巡检'
					}
				],
				type8list: [
					{
						name: '组套'
					},
					{
						name: '批量单'
					},
					{
						name: '辅助作业'
					}
				],
				type9list: [
					{
						name: '非机械'
					},
					{
						name: '叉（抱）车'
					}
				],
				worklist: [
					{
						name: '卸车'
					},
					{
						name: '装车'
					},
					{
						name: '拣货'
					},
					{
						name: '转仓装车'
					},
					{
						name: '转仓卸车'
					},
					
					{
						name: '入库'
					},
					{
						name: '移库'
					},
					{
						name: '出库'
					},
					{
						name: '领料'
					},
					{
						name: '理库'
					},
					{
						name: '废料'
					},
					{
						name: '批量作业'
					},
					{
						name: '跟车'
					},
					{
						name: '杂工'
					},
					{
						name: '其它'
					},
				],
				carlist: [
					{
						name: '海柜'
					},
					{
						name: '17.5米'
					},
					{
						name: '13.5米'
					},
					{
						name: '9.6米'
					},
					{
						name: '6.8米'
					},
					{
						name: '4.2米'
					}
				],
				modelist: [
					{
						name: '体积'
					},
					{
						name: '数量'
					},
					{
						name: '重量'
					},
					{
						name: '工时'
					}
				],
				clist:[],
				olist:[],
				show:false,
				show2:false,
				show3:false,
			    rules: {
					name: [
						{ 
							required: true,
							message: '请输入作业名称', 
							trigger: 'blur',
						}
					],
					volume: [
						{ 
							required: true,
							message: '请输入体积', 
							trigger: 'blur',
						},
						{ 
							type:'number',
							message: '请输入正确的数据类型', 
							trigger: 'blur',
						}
					],
					nums: [
						{ 
							required: true,
							message: '请输入数量', 
							trigger: 'blur',
						},
						{
							type:'number',
							message: '请输入正确的数据类型', 
							trigger: 'blur'
						}
					],
					weights: [
						{ 
							required: true,
							message: '请输入重量', 
							trigger: 'blur',
						},
						{
							type:'number',
							message: '请输入正确的数据类型', 
							trigger: 'blur',
						}
					]
				}
			};
		},
		onLoad(open) {
			let year = new Date().getFullYear();//年
			let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;
			let day = new Date().getDate();//日
			this.form.v1 =  year + '-' + (month >= 10 ? month : '0' + month) + '-' + (day >= 10 ? day : '0' + day);
			this.$http.post('/rest/operate/list',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				this.form.operateId = res.data.data[0].id
				this.form.operateName = res.data.data[0].operateName
				for(let item1 of res.data.data) {
					let obj = {}
					obj.value = item1.id
					obj.label = item1.operateName
					this.olist.push(obj)
				}
			})
			this.$http.post('/rest/customer/list',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				this.form.outCustomerId = res.data.data[0].id
				this.form.outCustomerName = res.data.data[0].customerName
				for(let item1 of res.data.data) {
					let obj = {}
					obj.value = item1.id
					obj.label = item1.customerName
					this.clist.push(obj)
				}
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods:{
			scan(){
				const _this = this; 
				uni.scanCode({
					onlyFromCamera: false,
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let customerNumber = _this.form.customerNumber
						if(customerNumber != ''){
							customerNumber = _this.form.customerNumber.replace(','+res.result,'').replace(res.result,'')
							if(customerNumber != ''){
								_this.form.customerNumber = customerNumber+','+res.result
							} else {
								_this.form.customerNumber = res.result
							}
						} else {
							_this.form.customerNumber = res.result
						}
					}
				});
			},
			// 选中某个单选框时，由radio时触发
			radioChange(e) {
				console.log(e);
			},
			// 选中某个单选框时，由radio时触发
			radioChange3(e) {
				console.log(e);
			},
			// 选中某个单选框时，由radio时触发
			radioChange2(e) {
				console.log(e);
			},
			radioChange4(e) {
				console.log(e);
			},
			radioChange5(e) {
				console.log(e);
			},
			radioChange6(e) {
				console.log(e);
			},
			radioChange7(e) {
				console.log(e);
			},
			radioChange8(e) {
				console.log(e);
			},
			radioChange9(e) {
				console.log(e);
			},
			radioChange10(e) {
				console.log(e);
			},
			radioChange11(e) {
				console.log(e);
			},
			radioChange12(e) {
				console.log(e);
			},
			getC(){
				this.show = true
			},
			getO(){
				this.show2 = true
			},
			getT(){
				this.show3 = true
			},
			confirm(e) {
				this.form.outCustomerId = e[0].value
				this.form.outCustomerName = e[0].label
			},
			confirm2(e) {
				this.form.operateId = e[0].value
				this.form.operateName = e[0].label
			},
			change(e) {
				this.form.v1 = e.result
				console.log(e);
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if(typeof this.form.operateId == 'undefined' || this.form.operateId == null || this.form.operateId == ""){
							uni.showToast({
								title: "请选择项目部",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.outCustomerId == 'undefined' || this.form.outCustomerId == null || this.form.outCustomerId == ""){
							uni.showToast({
								title: "请选择客户",
								icon: 'none'
							})
							return
						}
						if(this.form.taskWork != '卸车' && this.form.taskWork != '装车'){
							this.form.carModel = ''
						}
						if(this.form.taskWork == '卸车' || this.form.taskWork == '装车'){
							if(typeof this.form.customerNumber == 'undefined' || this.form.customerNumber == null || this.form.customerNumber == ""){
								uni.showToast({
									title: "请输入客户单号",
									icon: 'none'
								})
								return
							}
						}
						if(this.form.taskWork == '跟车'){
							if(typeof this.form.v9 == 'undefined' || this.form.v9 == null || this.form.v9 == ""){
								uni.showToast({
									title: "请输入门店数",
									icon: 'none'
								})
								return
							}
						}
						uni.showLoading({
							title: '正在提交'
						})
						this.$http.post('/rest/task/add',{
						},{
							params: this.form,
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							uni.hideLoading()
							if(res.data.code == 200) {
								this.$refs.uToast.show({
									title: '操作成功',
									type: 'success'
								})
								setTimeout(() => {
									this.$Router.pushTab({ 
										name: 'index',
									})
								}, 1000)
							} else {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'error'
								})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	
	.wrap {
		padding: 30rpx;
	}
</style>
